const mongoose = require("mongoose");
module.exports.getAllCarsInfor3 = async ({ currentPage, pageSize }) => {
    const allData = await mongoose.model("carModel").find().limit(pageSize - 0).skip((currentPage - 1) * pageSize)
    const allNumber = await mongoose.model("carModel").find().countDocuments()
    return [allData, allNumber]
}
module.exports.getCheckingCars3 = async (reqData) => {
    const { checkState, pageSize, currentPage } = reqData
    const allData = await mongoose.model("carModel").find({ checkState, rentalState: { $ne: "2" } }).limit(pageSize - 0).skip((currentPage - 1) * pageSize)
    const allNumber = await mongoose.model("carModel").find({ checkState, rentalState: { $ne: "2" } }).countDocuments()
    return [allData, allNumber]
}
module.exports.getSolidOutCars3 = async (reqData) => {
    const { rentalState, pageSize, currentPage } = reqData
    console.log(rentalState)
    allData = await mongoose.model("carModel").find({ rentalState }).limit(pageSize - 0).skip((currentPage - 1) * pageSize)
    const allNumber = await mongoose.model("carModel").find({ rentalState }).countDocuments()
    return [allData, allNumber]
}
module.exports.getOneCar3 = async (reqData) => {
    return await mongoose.model("carModel").find(reqData)
}

module.exports.getUserCar3 = async (ownerid) => {
    return await mongoose.model("carModel").find(ownerid)
    .populate("ownerid")
}


module.exports.changeState3 = async (reqData) => {
    const { _id } = reqData
    return await mongoose.model("carModel").updateOne({ _id }, reqData)
}
module.exports.addCarInfor3 = async (reqData) => {
    return await mongoose.model("carModel").create(reqData)
}
module.exports.searchCars3 = async (reqData) => {
    const {carnumber,brank,type,pageSize,currentPage}=reqData
    let o = {};
    if(carnumber){
        o.carnumber = carnumber;
    }else if(brank){
        o.brank = brank;
    }else{
        o.type=type
    }
        allData  =await  mongoose.model("carModel").find(o).limit(pageSize-0).skip((currentPage-1)*pageSize)
        allNumber = await  mongoose.model("carModel").find(o).countDocuments();
    console.log([reqData])
    return [allData,allNumber]
}